﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EMR_System.Controls;

namespace EMR_System.Forms
{
    public partial class AppointmentForm : Form
    {
        private List<User> docs;
        private List<Patient> patients;
        private string selectedPatient;
        private string selectedDoctor;
        private int apptID;

        public AppointmentForm()
        {
            InitializeComponent();
            this.Text = "Add New Appointment";

            selectedPatient = null;
            selectedDoctor = null;

            apptID = -1;
            addNoteBtn.Enabled = false;
        }

        public AppointmentForm(Appointment a)
        {
            InitializeComponent();
            this.Text = "Update Appointment";

            Patient p = Controllers.PatientController.GetPatient(a.PatientID);
            selectedPatient = String.Format("{0} {1} ({2})", p.FirstName, p.LastName, p.ID);
            //patientDD.Enabled = false;

            User doc = Controllers.UserController.GetUser(a.Doctor);
            selectedDoctor = String.Format("{0} {1} ({2})", doc.FirstName, doc.LastName, doc.Username);
            //doctorDD.Enabled = false;

            datePicker.Value = a.Date;
            //datePicker.Enabled = false;

            isNewPatientCB.Checked = a.IsNewPatient;
            //isNewPatientCB.Enabled = false;

            durationSpinner.Value = a.Duration;
            //durationSpinner.Enabled = false;

            reasonTB.Text = a.Reason;
            //reasonTB.Enabled = false;

            addAppointmentBtn.Text = "Update";

            apptID = a.ID;
        }

        private void addAppointmentBtn_Click(object sender, EventArgs e)
        {
            if (addAppointmentBtn.Text == "Add")
            {
                int ID = EMR_System.Controllers.AppointmentController.ScheduleAppointment(new Appointment(patients[patientDD.SelectedIndex].ID, docs[doctorDD.SelectedIndex].Username, datePicker.Value, isNewPatientCB.Checked, Convert.ToInt32(durationSpinner.Value), reasonTB.Text));

                if (ID == -1)
                {
                    MessageBox.Show("The appointment was not successfully added to the system. Please try again");
                    //ResetForm();
                }
                else
                {
                    MessageBox.Show(String.Format("Appointment {0} successfully added!", ID));
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("Update functionality not supprted yet", "Unsupported Operation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AppointmentForm_Load(object sender, EventArgs e)
        {
            this.noteTableAdapter.Fill(this.eMR_SystemDataSet1.Note);

            docs = Controllers.UserController.GetDoctors();
            
            foreach (User doc in docs)
            {
                doctorDD.Items.Add(String.Format("{0} {1} ({2})", doc.FirstName, doc.LastName, doc.Username));
            }

            if (selectedDoctor != null)
                doctorDD.SelectedItem = doctorDD.Items[doctorDD.Items.IndexOf(selectedDoctor)];

            patients = Controllers.PatientController.GetPatients();

            foreach (Patient p in patients)
            {
                patientDD.Items.Add(String.Format("{0} {1} ({2})", p.FirstName, p.LastName, p.ID));
            }

            if (selectedPatient != null)
                patientDD.SelectedItem = patientDD.Items[patientDD.Items.IndexOf(selectedPatient)];
        }

        private void addNoteBtn_Click(object sender, EventArgs e)
        {
            new NoteForm(apptID).ShowDialog();

            this.noteTableAdapter.Fill(this.eMR_SystemDataSet1.Note);
        }
    }
}
